'''
Created on 03.11.2013

@author: Liesa
'''
import doctest
import trunk.data
import os


"""Function for reading input and writing output"""
def filestuff(input, output):
    with open(input) as f:
        with open(output, "w") as g:
            for line in f:
                line = line.split()
                for word in line:
                    g.write("%s " % lemmatizer(word))
                
         
def lemmatizer(word):
    """
    Actual lemmatizer function, rule-based
    
    >>> lemmatizer("slowly/ADV")
    'slow/ADJ'
    
    >>> lemmatizer("She/PRON")
    'she/PRON'

    """
    
    pos = word.split("/")[1]
    word = word.split("/")[0].lower()
    
    same = ["PRON", "CONJ"]
    if pos in same:
        lemma = word+"/"+pos
        return lemma
    else:
        if pos == "VB" and word[-1] == "s":
            lemma = word[:-1]+"/"+pos
            return lemma
        
        if pos == "ADV" and word[-2:] == "ly":
            lemma = word[:-2]+"/ADJ"
            
            return lemma
    


if __name__ == '__main__':
    filestuff(os.path.join(trunk.data.__path__[0], 'input.txt'), os.path.join(trunk.data.__path__[0], 'output.txt'))
    print("Lemmatisation complete.")
    
    print(doctest.testmod())